package com.bluebridgecup.BasicAlgorithms.twoDimensionPrefixSumAndDifference;

import java.io.*;
import java.util.*;

// 类名：LightOperationCalculator，用于计算蓝桥公园灯光操作后的亮度情况
public class LightOperationCalculator {
    static int N = 1010;
    static long[][] a = new long[N][N];
    static long[][] s = new long[N][N];
    static FastReader in = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) {
        solve();
        out.flush();
    }

    // solve方法：核心处理逻辑方法
    // 功能：
    // 1. 首先从输入读取公园区域的行数n、列数m以及亮度减少限制值c。
    // 2. 接着通过两层循环读取每盏灯的初始亮度并存入数组a，同时计算并存储前缀和到数组s。
    // 3. 然后读取操作次数q。
    // 4. 针对每次操作，读取操作矩形区域的左上角坐标(x1, y1)、右下角坐标(x2, y2)以及开关按下次数k。
    // 5. 对k取模处理（k = k % (c + 1)），以符合题目中亮度变化的循环规则。
    // 6. 最后根据前缀和数组s计算并输出指定矩形区域内所有灯的总亮度。
    static void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int c = in.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                a[i][j] = in.nextInt();
                s[i][j] = a[i][j] + s[i][j - 1] + s[i - 1][j] - s[i - 1][j - 1];
            }
        }
        int q = in.nextInt();
        while (q-- > 0) {
            int x1 = in.nextInt();
            int y1 = in.nextInt();
            int x2 = in.nextInt();
            int y2 = in.nextInt();
            int k = in.nextInt();
            k = k % (c + 1);
            out.println(s[x2][y2] - s[x2][y1 - 1] - s[x1 - 1][y2] + s[x1 - 1][y1 - 1] - (long) (x2 - x1 + 1) * (y2 - y1 + 1) * k);
        }
    }

    static class FastReader {
        static BufferedReader br;
        static StringTokenizer st;

        FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            String str = "";
            while (st == null || !st.hasMoreElements()) {
                try {
                    str = br.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
                st = new StringTokenizer(str);
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}